#include "SenderEngine.h"
#include <thread>
#include "Logger.h"
#include "OnTestMsg.h"
#include "XnccType.h"
using namespace std::placeholders;
using namespace xncc::user;
using namespace xncc::types;

#define ONE_SECOND 1000000

SenderEngine::SenderEngine() : EventEngine(1)
{
}

void SenderEngine::init()
{
    xncc::types::timer_id_t id = setIntervalFuncFromNow(ONE_SECOND, std::bind(&SenderEngine::handleTestMsg, this));
    if (id == xncc::types::INVALID_TIMER_ID) {
        LOG_ERROR(CONSOLE_LOG) << "create timer id failed ";
    }
}

void SenderEngine::handleTestMsg()
{
    OnTestMsg  realRsp;
    static int num = 1;
    // if (num == 2) {
    //     return;
    // }
    realRsp.init(++num);

    LOG_INFO(CONSOLE_LOG) << "sender engine send  " << num;
    sendData(realRsp.data_, realRsp.len(), MessageTraits<OnTestMsg>::ID);

    for (auto id : GetPubIds()) {
        sendAny(realRsp.data_, realRsp.len(), MessageTraits<NewTestMsg>::ID, id);
        LOG_INFO(CONSOLE_LOG) << "sender engine send  " << num << " to pub id " << id;
    }
}
